package customer.zspt.cn.customer.Services;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;

import com.google.gson.Gson;

import org.java_websocket.client.WebSocketClient;
import org.java_websocket.drafts.Draft_17;
import org.java_websocket.handshake.ServerHandshake;

import java.net.URI;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import customer.zspt.cn.customer.bean.GetGroupChatMsgDao;
import customer.zspt.cn.customer.constant.constant;
import customer.zspt.cn.customer.sqlites.dao.chatMsgSqliteDao;
import customer.zspt.cn.customer.utils.AddDateToSqliteUtils;
import customer.zspt.cn.customer.utils.ChatRecordSPUtils;
import customer.zspt.cn.customer.utils.SPUtils;
/**
 * websocket连接
 */
public class WebSocketService extends Service {
    public String token = "";
    public static ExecutorService executorService = Executors.newSingleThreadExecutor();
    public static WebSocketClient webSocketClient;
    private chatMsgSqliteDao msgSqliteDao = null;
    private Gson gson;
    private static final int connectSocket = 0;
    private static boolean IsOpen = false;
    private static Context mContext;
    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            if(msg.what ==  connectSocket){
                if(!IsOpen){
                    conn(token);
                }
            }
        }
    };
    public static void startService(Context context){
        mContext = context;
        Intent intent = new Intent();
        intent.setClass(context,WebSocketService.class);
        context.startService(intent);
    }
    public static void conn(final String token) {
        executorService.execute(new Runnable() {
            @Override
            public void run() {
                Map<String, String> headers = new HashMap();
                webSocketClient = new WebSocketClient(URI.create("ws://sk.yomiyun.com:9321/?" + token + "&kefu"), new Draft_17(), headers, 10) {
                    @Override
                    public void onOpen(ServerHandshake serverHandshake) {
                        IsOpen = true;
                    }
                    @Override
                    public void onMessage(String s) {

                    }
                    @Override
                    public void onClose(int i, String s, boolean b) {
                        IsOpen = false;
                    }
                    @Override
                    public void onError(Exception e) {
                        System.out.println("client onError:" + e);
                    }
                };
                webSocketClient.connect();
            }
        });
    }
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
    public void onCreate() {
        super.onCreate();
        msgSqliteDao = new chatMsgSqliteDao(this);
        token = SPUtils.getString(this,"token","");
        gson = new Gson();
        if(!token.isEmpty()){
            conn(token);
        }
    }
    //建立連接
    @Override
    public void onDestroy() {
        super.onDestroy();
        webSocketClient.close();
    }
    private void addMsg(String data) {
        //群聊消息
        String history = ChatRecordSPUtils.getRecordString(this, constant.HISTROYCHATPEOPLE, constant.HISTROYCHATPEOPLE, "");
        GetGroupChatMsgDao getgroupChatMsgDao = gson.fromJson(data, GetGroupChatMsgDao.class);
        //添加聊天成员
        String replace = history.replace(getgroupChatMsgDao.data.id + ",", "");
        replace = getgroupChatMsgDao.data.id + ","+replace;
        ChatRecordSPUtils.setRecordString(this, constant.HISTROYCHATPEOPLE, constant.HISTROYCHATPEOPLE, replace);
        String count = ChatRecordSPUtils.getRecordString(this, getgroupChatMsgDao.data.id, getgroupChatMsgDao.data.id + "_count", "0");
        int DRcount = Integer.parseInt(count) + 1;
        ChatRecordSPUtils.setRecordString(this, getgroupChatMsgDao.data.id, getgroupChatMsgDao.data.id + "_count", DRcount + "");
        AddDateToSqliteUtils.addMsgToSqlite(getgroupChatMsgDao,msgSqliteDao);
    }
}

